package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.Address;
import cn.edu.scau.dropship.entity.AddressExample;
import cn.edu.scau.dropship.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping(value = "/insert{country,province,city,detail}")
    public String insert(@RequestParam("country") String country,
                         @RequestParam("province") String province,
                         @RequestParam("city") String city,
                         @RequestParam("detail") String detail) {
        Address address = new Address();
        address.setCountry(country);
        address.setProvince(province);
        address.setCity(city);
        address.setDetails(detail);
        int result = addressService.insert(address);
        if (result > 0)
            return "插入成功";
        return "插入失败";
    }

    @GetMapping(value = "/deleteById{id}")
    public String deleteById(@RequestParam("id") Integer id) {
        int result = addressService.deleteByPrimaryKey(id);
        if (result > 0)
            return "删除成功" + result;
        return "删除失败" + result;
    }

    @GetMapping(value = "/updateById{id,country,province,city,detail}")
    public String updateById(@RequestParam("id") Integer id,
                             @RequestParam("country") String country,
                             @RequestParam("province") String province,
                             @RequestParam("city") String city,
                             @RequestParam("detail") String detail) {
        Address address = new Address();
        address.setId(id);
        address.setCountry(country);
        address.setProvince(province);
        address.setCity(city);
        address.setDetails(detail);
        int result = addressService.updateByPrimaryKey(address);
        if (result > 0)
            return "修改成功" + result;
        return "修改失败" + result;
    }

    @GetMapping(value = "/selectById{id}")
    public Address selectById(@RequestParam("id") Integer id) {
        Address address = addressService.selectByPrimaryKey(id);
        return address;
    }

    @GetMapping(value = "/selectAll")
    public List<Address> selectAll() {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIdGreaterThan(0);
        List<Address> list = addressService.selectByExample(addressExample);
        return list;
    }
}
